package com.example.gaoyang.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.gaoyang.domain.SysRole;
import com.example.gaoyang.domain.SysRoleElement;
import com.example.gaoyang.domain.request.SysRoleElementRequest;
import com.example.gaoyang.domain.request.SysRoleRequest;
import com.example.gaoyang.domain.vo.SysRoleVO;
import com.example.gaoyang.enums.ResultEnum;
import com.example.gaoyang.exception.InvalidParameterException;
import com.example.gaoyang.mapper.SysRoleMapper;
import com.example.gaoyang.request.Result;
import com.example.gaoyang.service.SysRoleElementService;
import com.example.gaoyang.service.SysRoleService;
import com.example.gaoyang.util.ResultUtil;
import com.example.gaoyang.wrapper.SysRoleWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
@Transactional
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Resource
    private SysRoleElementService sysRoleElementService;

    @Override
    public void addSysRole(SysRoleRequest sysRoleRequest) {
        SysRole sysRole = BeanUtil.copyProperties(sysRoleRequest, SysRole.class);
        sysRole.setId(IdUtil.getSnowflake().nextId());
        sysRole.setCreateTime(new Date());
        this.save(sysRole);
    }

    @Override
    public void addRoleAndElement(SysRoleRequest sysRoleRequest) {
        // 新增角色
        SysRole sysRole = BeanUtil.copyProperties(sysRoleRequest, SysRole.class);
        sysRole.setId(IdUtil.getSnowflake().nextId());
        this.save(sysRole);
        // 新增角色系统元素关系
        SysRoleElementRequest sysRoleElementRequest = new SysRoleElementRequest();
        sysRoleElementRequest.setRoleId(sysRole.getId());
        sysRoleElementRequest.setElementIds(sysRoleRequest.getElementIds());
        sysRoleElementService.batchUpdateSysRoleElement(sysRoleElementRequest);
    }

    @Override
    public void removeSysRole(SysRoleRequest sysRoleRequest) {
        // id不能为空
        Long id = sysRoleRequest.getId();
        if (id == null) {
            throw new InvalidParameterException("参数id缺失");
        }
        this.removeById(id);
    }

    @Override
    public void removeRoleAndElement(SysRoleRequest sysRoleRequest) {
        // 角色id不能为空
        Long id = sysRoleRequest.getId();
        if (id == null) {
            throw new InvalidParameterException("参数id缺失");
        }
        this.removeById(id);
        // 删除权限系统元素关联数据
        sysRoleElementService.remove(new LambdaQueryWrapper<SysRoleElement>()
                .eq(SysRoleElement::getRoleId, id)
        );
    }

    @Override
    public List<SysRoleVO> getSysRoleList(SysRoleRequest sysRoleRequest) {
        LambdaQueryWrapper<SysRole> wrapper = this.createQueryWrapper(sysRoleRequest);
        List<SysRole> list = this.list(wrapper);
        return BeanUtil.copyToList(list, SysRoleVO.class);
    }

    @Override
    public IPage<SysRoleVO> getSysRolePage(IPage<SysRole> page, SysRoleRequest sysRoleRequest) {
        page.setCurrent(sysRoleRequest.getPageNum());
        page.setSize(sysRoleRequest.getPageSize());
        return new SysRoleWrapper().pageVO(this.page(page, this.createQueryWrapper(sysRoleRequest)));
    }

    @Override
    public void updateSysRole(SysRoleRequest sysRoleRequest) {
        // id不能为空
        Long id = sysRoleRequest.getId();
        if (id == null) {
            throw new InvalidParameterException("参数id缺失");
        }
        SysRole sysRole = BeanUtil.copyProperties(sysRoleRequest, SysRole.class);
        sysRole.setUpdateTime(new Date());
        this.updateById(sysRole);
    }

    @Override
    public void updateRoleAndElement(SysRoleRequest sysRoleRequest) {
        // 修改权限
        Long id = sysRoleRequest.getId();
        if (id == null) {
            throw new InvalidParameterException("参数id缺失");
        }
        SysRole sysRole = BeanUtil.copyProperties(sysRoleRequest, SysRole.class);
        sysRole.setUpdateTime(new Date());
        this.updateById(sysRole);
        // 修改系统元素关联数据
        SysRoleElementRequest sysRoleElementRequest = new SysRoleElementRequest();
        sysRoleElementRequest.setRoleId(id);
        sysRoleElementRequest.setElementIds(sysRoleRequest.getElementIds());
        sysRoleElementService.batchUpdateSysRoleElement(sysRoleElementRequest);
    }

    private LambdaQueryWrapper<SysRole> createQueryWrapper(SysRoleRequest sysRoleRequest) {
        return new LambdaQueryWrapper<SysRole>()
                .like(!StringUtils.isEmpty(sysRoleRequest.getRoleName()), SysRole::getRoleName, sysRoleRequest.getRoleName())
                .like(!StringUtils.isEmpty(sysRoleRequest.getRemark()), SysRole::getRemark, sysRoleRequest.getRemark())
                .orderByDesc(SysRole::getCreateTime);
    }
}
